//
//  MK_HomePageViewModel.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/24.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_HomePageViewModel.h"


@implementation MK_HomePageViewModel



///首页分类命令
-(RACCommand *)CategoryCommand{
    if (_CategoryCommand == nil) {
        _CategoryCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [self getHomePageCategoryData];
        }];
    }
    return _CategoryCommand;
}
///首页轮播器信息
-(RACCommand *)HotRoomCommand{
    _HotRoomCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [self getRecommendData];
    }];
    return _HotRoomCommand;
}
///首页其他分类的热点命令
-(RACCommand *)CategoryHotRoomCommandWithIDArr:(NSArray<NSString *>*)idArr{
    _CategoryHotRoomCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [self getHomePageCategoryHotRoomDataWithIdArr:idArr];
    }];
    return _CategoryHotRoomCommand;
}

///获得首页分类资源
-(RACSignal*)getHomePageCategoryData{
    self.state = MK_HomePageViewModel_State_Loading;
    return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [[MK_NetWorking sharedNetWorking] MK_getHomeCategoryModelArrWithBackBlock:^(NSArray<MK_HomeCategoryModel *> *res) {
            [subscriber sendNext:res];
            [subscriber sendCompleted];
        } andFailBlock:^(NSError *err) {
            self.state = MK_HomePageViewModel_State_FailFailed;
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}
///获得轮播器数据
-(RACSignal*)getRecommendData{
    ///轮播器信号
    RACSignal* slideSingal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [[MK_NetWorking sharedNetWorking] MK_getHomeSlideItemArrWithBackBlock:^(NSArray<MK_HomeSlideItem *> *res) {
            [subscriber sendNext:res];
            [subscriber sendCompleted];
        } andFailBlock:^(NSError *err) {
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    
    return slideSingal;
}


///获得首页分类的热点房间信息
-(RACSignal*)getHomePageCategoryHotRoomDataWithIdArr:(NSArray<NSString*>*)idArr{
    NSMutableArray<RACSignal*>* singalArr = [NSMutableArray new];
    RACSignal* hotRoomSingal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        [[MK_NetWorking sharedNetWorking] MK_getHotRoomDataWithBackBlock:^(NSArray<MK_HotRoomModel *> *res) {
            [subscriber sendNext:res];
            [subscriber sendCompleted];
        } andFailBlock:^(NSError *err) {
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    [singalArr addObject:hotRoomSingal];
    
    [idArr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        RACSignal* hotRoomSingal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            [[MK_NetWorking sharedNetWorking] MK_GetCategoryHotDataWithID:obj andBackBlock:^(NSArray<MK_HotRoomModel *> *res) {
                [subscriber sendNext:res];
                [subscriber sendCompleted];
            } andFailBlock:^(NSError *err) {
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        [singalArr addObject:hotRoomSingal];
    }];
    
    
    return [RACSignal combineLatest:singalArr];
}

@end
